$(function() {
    $("#login").click(function(event) {
        var username = $("#username").val();
        var pwd = $("input[name='password']").val();
        if(username=='' || pwd==''){
            var dialog1 = jqueryAlert({
                'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                'content': '请输入账号或密码！',
                'closeTime': 3000,
            })
            return;
        }
        var type=$('input:radio[name="type"]:checked').val();
        console.log(type);
        if(type==1){
            //服务器正确代码
            getToken();
        }else if(type==2){
            $.ajax({
                url: $('input[name="url"]').val() + 'Home/Login/Login',
                type: 'POST',
                dataType: 'json',
                data: {
                    username: username,
                    pwd: pwd
                },
                success: function(data) {
                    if (data.status) {
                        // var url=$("input[name='url']").val()+"Home/Login";'"+$("input[name='url']").val()+"Home/Login'
                        var t = JSON.parse(data.v);
                        // if (t['check_role'] == 1) {
                            if (dialog1) {
                                return dialog1.show();
                            }
                            var dialog1 = jqueryAlert({
                                'icon': $('input[name="url"]').val() + 'public/home/alert/img/right.png',
                                'content': data.msg,
                                'closeTime': 3000,
                            })
                             window.setTimeout("window.location.href='" + $("input[name='url']").val() + "Home/Index/Index/'", 1000);
                        // }

                    } else {
                        if (dialog1) {
                            return dialog1.show();
                        }
                        var dialog1 = jqueryAlert({
                            'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                            'content': data.msg,
                            'closeTime': 3000,
                        })
                    }
                }
            })
        }else{
            var dialog1 = jqueryAlert({
                'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                'content': '请选择登陆方式',
                'closeTime': 3000,
            })
        }
    });
})

function ps() {
    if (this.forms.password.type = "password") box.innerHTML = "<input type=\"html\" name=\"password\" size=\"20\" value=" + this.forms.password.value + ">";
    click.innerHTML = "<a href=\"javascript:txt()\" class=\"iconfont icon-open-eye\"></a>"
}

function txt() {
    if (this.forms.password.type = "text") box.innerHTML = "<input type=\"password\" name=\"password\" size=\"20\" value=" + this.forms.password.value + ">";
    click.innerHTML = "<a href=\"javascript:ps()\" class=\"iconfont icon-biyan\"></a>"
}



function getToken() {
    $.post(getRootPath()+"/CommonApi/CommonZjipcCa/getToken", {},
        function(ret, err) {
            if (ret.status) {
                login(ret.v);
            } else {
                var dialog1 = jqueryAlert({
                    'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                    'content': ret.msg,
                    'closeTime': 3000,
                })
            }
        },
        "json"); //这里返回的类型有：json,html,xml,text
}

function login(token) {
    var iv = "e6db271db12d4d47";
    var key = "9cd5b4cf899492079cd5b4cf89949207"; //秘钥。长度32的16进制字符串。
    var cryptkey = CryptoJS.enc.Utf8.parse(key);
    $.ajax({
        type: 'POST',
        url: $('#zjipc_auth_url').val() + 'Api/User',
        data: {
            appid: $('#zjipc_app_id').val(),
            method: 'userInfo',
            token: token,
            username: $("#username").val(),
            password: aesEncrypt($('#pwd').val(), cryptkey, iv)
        },
        async: false,
        dataType: 'jsonp',
        success: function(ret) {
            var t=JSON.parse(ret.v);
         if(ret.status){
              // alert(JSON.stringify(t));
              $.post(getRootPath()+"/Home/Login/doLogin", {userinfo:t},
                  function(ret, err) {
                      if (ret.status) {
                        var dialog1 = jqueryAlert({
                            'icon': $('input[name="url"]').val() + 'public/home/alert/img/success.png',
                            'content': "登录成功!",
                            'closeTime': 3000
                        })
                        window.setTimeout("window.location.href='" + $("input[name='url']").val() + "Home/Index/Index/'", 1000);
                    } else {
                      var dialog1 = jqueryAlert({
                          'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                          'content': ret.msg,
                          'closeTime': 3000
                      })
                  }
              },
                  "json"); //这里返回的类型有：json,html,xml,text
          }
          else{
            var dialog1 = jqueryAlert({
                'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                'content': ret.msg,
                'closeTime': 3000,
            })
        }
    }
});
}